#include <iostream>


using namespace std;

class bank:public memory{
    public:
        int banks;
        int* blocked;
        int currentBank;
        bank(int numberofbanks);
        ~bank();
        void mem(bus2_t* line);
        void wrapup();
};

bank::bank(int numberofbanks){

    cycles = 0;
    cache = 0;
    misses = -1;
    conflicts = -1;
    banks = numberofbanks;
    blocked = new int[banks];

    for (int i=0; i<banks; i++)
        blocked[i] = 0;

    

}

//Calls process to process the line and updates cycles and conflicts.
void bank::mem(struct bus2_t* line){

    process(line);
    currentBank = (line->address % (banks*4))/4;

    //If a banked is blocked, the programm stalls. Cycles keeps updating with 1
    //untill bank is free. The cycle time of the banks is lowerd by 1 every cycle.
    while (blocked[currentBank] > 0){
        cycles++;
        for (int i=0; i<banks; i++){
            if (blocked[i] > 0)
                blocked[i]--;
        }
    }

    // If the bank is free, the cycle time of the bank is set to 9: Random acces
    //time + transfer and conflict is raised by 1.
    blocked[currentBank] = 9;
    conflicts++;

    //cycle time of all the banks is lowerd by 1
    for (int i=0; i<banks; i++){
        if (blocked[i] > 0)
            blocked[i]--;
    }

}

//calculates the bandwith and outputs it.
void bank::wrapup(){

    int max = 0;
    for (int i=0; i<banks; i++){
        if (blocked[i] > max)
            max = blocked[i];
    }

    cycles += max;

    bytes = cache * 4;
    bandwidth = (float)bytes/cycles;
    cout <<"output of bank with banksize " << banks << "\n Bank conflicts: " <<
    conflicts << "\n Bandwidth: " << bandwidth << endl << endl;
}
